package com.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.Help;
import com.entity.Notice;
import com.service.NoticeService;
import com.util.StatusCode;
import com.vo.LayuiPageVo;
import com.vo.ResultVo;

@Controller
public class NoticeController {
	@Autowired
	NoticeService noticeService;
	/**
     *后台管理界面，查看审核通过的校园新闻
     * **/
    @GetMapping("/con/audschoolnews")
    public String AudSchoolNews (){
        return "/admin/SchoolNews/AudNotice";
    }
    
    /**
     *后台管理界面，查看审核未通过的校园新闻
     * **/
    @GetMapping("/con/Unaudschoolnews")
    public String UnAudSchoolNews(){
        return "/admin/SchoolNews/UnAudNotice";
    }
    
    /**
     *前端管理界面，查看审核未通过的校园新闻
     * **/
    @RequestMapping("/html/contacts_detail")
    public String help() {
    	 return "/common/contacts_detail";
    	}
    
    /**
     * 首页头部校园新闻跳转页面跳转
     * */
    @GetMapping("/SchoolNews")
    public String contacts(){
        return "/common/contacts";
    }
	/**
     * 后台分页查看新闻列表
     * 1.前端传入页码、分页数量
     * 2.查询分页数据
     */
    @ResponseBody
    @GetMapping("/notice/queryall")
    public LayuiPageVo queryAllNotices(int limit, int page) {
        List<Notice> Notices = noticeService.queryAllNotices((page - 1) * limit, limit);
        Integer dataNumber = noticeService.LookNoticeCount();
        return new LayuiPageVo("",0,dataNumber,Notices);
    }
   
	/**插入公告*/
    @ResponseBody
    @PostMapping("/notice/insert")
    public ResultVo InsertNotice (@RequestBody Notice notice) {
    	
    	Date dNow = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowdate = ft.format(dNow);
		notice.setTime(nowdate);
		Integer i =noticeService.insertNotice(notice); 
		if (i == 1){
            return new ResultVo(true, StatusCode.OK,"公告发布成功");
        }
        return new ResultVo(false,StatusCode.ERROR,"公告发布失败，请重新发布");
		 
    }
    /**删除公告*/
    @ResponseBody
    @PutMapping("/notice/delete/{id}")
    public ResultVo DeleteNoticeById (@PathVariable("id") String  id) {
        Integer i = noticeService.deleteNoticeById(id);
        if (i == 1){
            return new ResultVo(true, StatusCode.OK,"设置成功");
        }
        return new ResultVo(true, StatusCode.ERROR,"设置失败");
    }
    /**查询前五条公告 */
    @ResponseBody
    @PutMapping("/notice/look")
    public ResultVo LookNotice () {
    	List<Notice> notices = noticeService.queryNotice();
        if (notices != null){
            return new ResultVo(true, StatusCode.OK, "设置成功" ,notices);
        }
        return new ResultVo(true, StatusCode.ERROR,"设置失败",notices);
    }
}
	